<template>
  <el-container style="height: 100%">
    <el-header>
      <el-input
          style="width: 220px;margin-left: 200px"
          placeholder="请输入文件名"
          v-model="input"
          clearable/>
      <el-button type="primary" style="margin-left: 60px" @click="clickQuery">查询</el-button>
      <el-button type="primary" style="margin-left: 60px" @click="newModel">新建</el-button>
    </el-header>
    <el-main>

      <el-table
          :data="tableData"
          border
          v-loading="loading"
          stripe
          height="100%"
          style="width: 100%;">
        <el-table-column
            type="index"
            width="50">
        </el-table-column>
        <el-table-column
            prop="uuid"
            label="ID"
        >
        </el-table-column>
        <el-table-column
            prop="name"
            label="模型名称"
        >
        </el-table-column>
        <el-table-column
            prop="time"
            label="创建时间"
        >
        </el-table-column>
        <el-table-column
            fixed="right"
            label="操作"
            width="100">
          <template slot-scope="scope">
            <el-button @click="handleEdit(scope.row)" type="text" size="small">编辑</el-button>
            <el-button @click="handleDelete(scope.row)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
    <el-footer style="text-align: center">
      <el-pagination
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage"
          :page-size="pageSize"
          layout="total, prev, pager, next"
          :total="total">
      </el-pagination>
    </el-footer>
  </el-container>
</template>

<script>
import api_story from "@/api/api_story";

export default {
  name: "ModelManager",
  components: {},
  data() {
    return {
      tableData: [],
      currentPage: 1,
      total: 0,
      input: "",
      pageSize: 10,
      loading: false,
      showUpload: false
    }
  },
  mounted() {
    this.clickQuery()
  },
  methods: {

    clickQuery() {
      this.currentPage = 1;
      this.updateCount();
    },
    newModel() {
      this.$router.push({path: '/', query: {}})
    },
    handleEdit(row) {
      this.$router.push({path: '/', query: {id: row.uuid}})
    },
    updateCount() {
      this.loading = true;
      api_story.getStoryCount({
        key: this.input
      }).then(
          res => {
            this.total = res.data;
            this.updateTable()
          }
      ).catch(err => {
        console.error(err)
        this.$message.error(err);
      }).finally(() => {
        this.loading = false
      })
    },
    updateTable() {
      this.loading = true;
      api_story.queryByPage({
        pageSize: this.pageSize, pageCur: this.currentPage, condition: {
          name: this.input
        }
      }).then(
          res => {
            this.tableData = res.data;
          }
      ).catch(err => {
        console.error(err)
        this.$message.error(err);
      }).finally(() => {
        this.loading = false
      })
    },

    //删除模型
    handleDelete(row) {
      this.loading = true;
      api_story.deleteModel(row.uuid).then(res => {
        if (res.code === 0) {
          this.$message.success("删除成功");
          this.updateCount()
        } else {
          this.$message.error(res.msg);
        }
      }).catch(err => {
        console.error(err)
        this.$message.error(err);
      }).finally(() => {
        this.loading = false;
      })
    },

    //当前页变化
    handleCurrentChange() {
      this.updateTable()
    }
  }
}
</script>

<style scoped>

</style>
